#include<stdio.h>
#include<stdlib.h>
typedef struct Node* List;
struct Node {
	List Next;
	int b;
	int a;
};
List Read() {
	List L = (List)malloc(sizeof(struct Node));
	List head = L;
	int n;
	int i = 0;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		int a;
		int b;
		List t = (List)malloc(sizeof(struct Node));
		scanf("%d %d", &a, &b);
		t->a = a;
		t->b = b;
		L->Next = t;
		L = L->Next;
	}
	L->Next = NULL;
	return head->Next;
}
List addition(List L1, List L2) {
	List tL1 = L1;
	List tL2 = L2;
	List add = (List)malloc(sizeof(struct Node));
	add->Next = NULL;
	List head = add;
	List t;
	while (tL1 && tL2) {
		t = (List)malloc(sizeof(struct Node));
		if (tL1->b == tL2->b) {
			t->a = tL1->a + tL2->a;
			t->b = tL1->b;
			tL1 = tL1->Next;
			tL2 = tL2->Next;
		}
		else if (tL1->b < tL2->b) {
			t->a = tL2->a;
			t->b = tL2->b;
			tL2 = tL2->Next;
		}
		else if (tL2->b < tL1->b) {
			t->a = tL1->a;
			t->b = tL1->b;
			tL1 = tL1->Next;
		}
		add->Next = t;
		add = add->Next;
	}
	if (tL1)
		add->Next = tL1;
	else if (tL2)
		add->Next = tL2;
	return head->Next;
}
List multiplication(List L1, List L2) {
	List tL1 = L1;
	List tL2 = L2;
	List mul = (List)malloc(sizeof(struct Node));
	mul->Next = NULL;
	List head = mul;
	List t;
	for (; tL1; tL1 = tL1->Next)
		for (tL2 = L2; tL2; tL2 = tL2->Next) {
			t = (List)malloc(sizeof(struct Node));
			t->a = tL1->a * tL2->a;
			t->b = tL1->b + tL2->b;
			t->Next = NULL;
			head = addition(t, mul);
			mul = head;
		}
	return head;
}
void Print(List L) {
	List t = L;
	int flag = 1;
	for (; t; t = t->Next) {
		if (!flag && t->a)
			printf(" ");
		if (t->a) {
			printf("%d %d", t->a, t->b);
			flag = 0;
		}
	}
	if (flag)
		printf("0 0");
	printf("\n");
}
int main() {
	List L1, L2, add, mul;
	L1 = Read();
	L2 = Read();
	add = addition(L1, L2);
	mul = multiplication(L1, L2);
	Print(mul);
	Print(add);
	return 0;
}